<?php

class MainController extends AppController {
    public $name = 'main';
    public $uses = array('User', 'Post');

    private $title = 'RPG Game';

    public $helpers = array('Html', 'Form', 'Js', 'Session', 'Text');

    public function index() {
        $this -> set('title_for_layout', $this -> title);

        $total = $this -> Post -> find('count');

        if ($total > 0) {
            $posts = $this -> Post -> find('all');
            $this -> set('posts', $posts);
            $authors = array();
            foreach ($posts as $post) {
                $user_id = $post['Post']['user_id'];
                $author = $this -> User -> findById($user_id);
                $authors[] = $author['User']['username'];
            }
            $this -> set('authors', $authors);
        }
    }

    public function rules() {
        $this -> set('title_for_layout', $this -> title . ': Rules');
    }

    public function about() {
        $this -> set('title_for_layout', $this -> title . ': About us');
    }

    public function register() {
        $this -> set('title_for_layout', $this -> title . ': Register');

        if ($this -> request -> is('post')) {
            $this -> User -> create();
            if ($this -> User -> save($this -> request -> data)) {
                $this -> Session -> setFlash('User has been created');
                $this -> redirect(array('action' => index));
            } else {
                $this -> Session -> setFlash("Unable to register the user. Please try again");
            }
        }
    }

    public function login() {
        if ($this -> request -> is('post')) {
            $user = $this -> User -> findByUsername($this -> request -> data['User']['user']);

            if (!empty($user)) {
                $hashed_password = AuthComponent::password($this -> request -> data['User']['pass']);
                if ($user['User']['password'] === $hashed_password) {
                    unset($hashed_password);
                    if ($this -> Auth -> login($user['User'])) {
                        $this -> redirect($this -> Auth -> redirect());
                    }
                }
            }
            $this -> Session -> setFlash(__('Username or password is incorrect'), 'default', array(), 'auth');
            $this -> redirect(array('action' => 'index'));
        }
    }

    public function beforeFilter() {
        parent::beforeFilter();
        $this -> Auth -> allow();
        if ($this -> Auth -> user() != NULL) {
            $this -> redirect($this -> Auth -> redirect());
        }
    }

    private function generateRandomChar() {
        $character = mt_rand(0, 2);
        if ($character == 0) {
            return chr(97 + mt_rand(0, 25));
        } elseif ($character == 1) {
            return chr(65 + mt_rand(0, 25));
        } else {
            return mt_rand(0, 9);
        }
    }

    private function generateRandomPassword() {
        $lenght = mt_rand(8, 11);
        $password = '';
        for ($i = 0; $i < $lenght; $i++) {
            $password = $password . $this->generateRandomChar();
        }
        return $password;
    }

    public function forgot_password() {
        if ($this -> request -> is('post')) {
            $emailInput = $this -> request -> data['email']['email'];
            $user = $this -> User -> findByEmail($emailInput);
            $randomString = $this->generateRandomPassword();
            $user['User']['password'] = $randomString;
            $this -> User -> save($user['User']);
            $this -> set('email', $randomString);
            unset($randomString);
        }
    }

}
?>

